import datetime
import logging
import os
import time
import wsgiref.handlers
import random

from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

import py.datamodel

class Extractor(webapp.RequestHandler):
    def get(self):
       
        self.response.headers['Content-Type'] = 'text/html'
        visitorList = db.GqlQuery("select * from Visit limit " + self.request.get("limit"))
        self.response.out.write("<html><body>")
        for x in visitorList:
            self.response.out.write("<form method = 'POST'>\
                                    <textarea name = 'cookie'>%s</textarea>\
                                    <textarea name = 'enterTime'>%s</textarea>\
                                    <textarea name = 'exitTime'>%s</textarea>\
                                    <textarea name = 'ip'>%s</textarea>\
                                    <textarea name = 'ownerId'>%s</textarea>\
                                    <textarea name = 'param'>%s</textarea>\
                                    <textarea name = 'sessionId'>%s</textarea>\
                                    <textarea name = 'owner'>%s</textarea>\
                                    <textarea name = 'viewer'>%s</textarea>\
                                    <textarea name = 'viewerId'></textarea>\
                                    </form><hr>\n" 
 % (x.cookie, x.enterTime, x.exitTime, x.ip, x.ownerId, x.param, x.sessionId, x.owner, x.viewer));
            if self.request.get("delete") == "true":  
                x.delete()
        self.response.out.write('<script type="text/javascript" src= "http://yui.yahooapis.com/combo?2.5.2/build/utilities/utilities.js&2.5.2/build/container/container-min.js&2.5.2/build/tabview/tabview-min.js&2.5.2/build/button/button-min.js&2.5.2/build/json/json-min.js"></script>');
        self.response.out.write("\n<script src = '/js/migration.js' type = 'text/javascript'></script>");
        self.response.out.write("</body></html>");
        
if __name__ == '__main__':
  application = webapp.WSGIApplication(
      [
        ('/.*', Extractor)
      ],
      debug=True)

  wsgiref.handlers.CGIHandler().run(application)
